package com.example.test02mybatisplus.common;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Author: fxy
 * @Date: 2022/05/07/16:40
 * @Description:
 */
@Data
public class QueryConditionBase {
    @ApiModelProperty(
            value = "当前页",
            example = "1"
    )
    private Integer currentPage = 1;
    @ApiModelProperty(
            value = "每页数量,默认为10",
            example = "10"
    )
    private Integer pageSize = 10;
    @ApiModelProperty("排序条件")
    private String orderBy;
    @ApiModelProperty("租户Id")
    private String tenantId;
    @ApiModelProperty("删除标志, 0-正常，1删除")
    private Integer delFlag = 0;
    @ApiModelProperty("分页开启标志, true-开启，false-关闭，默认-true-开启分页")
    private Boolean pageFlag = true;

    public Integer getCurrentPage() {
        return this.currentPage;
    }

    public QueryConditionBase setCurrentPage(Integer currentPage) {
        if (currentPage != null) {
            this.currentPage = currentPage > 1 ? currentPage : 1;
        }

        return this;
    }

    public Integer getPageSize() {
        return this.pageSize != null ? this.pageSize : this.currentPageSize();
    }

    public void generatePageInfo() {
        if (this.pageFlag != null && this.pageFlag) {
            if (this.currentPage == null || this.currentPage <= 0) {
                this.currentPage = 1;
            }

            if (StringUtils.isNotBlank(this.getOrderBy())) {
                PageHelper.startPage(this.currentPage, this.pageSize, this.getOrderBy());
            } else {
                PageHelper.startPage(this.currentPage, this.pageSize);
            }
        } else {
            PageHelper.clearPage();
        }

    }

    private int currentPageSize() {
        return this.pageSize == null ? 20 : this.pageSize;
    }

    public QueryConditionBase() {
    }
}
